package com.yiren.dbaa.module.practice_set.line.presenter;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;

import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.backstage.db.dao.controller.GradeDbController;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.practice_set.line.contract.LineReleaseContract;
import com.yiren.dbaa.module.practice_set.line.model.LineReleaseModel;
import com.yiren.dbaa.module.practice_set.PracticeCarSetActivity;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

/**
 * @author lwb
 * @date : 2022/11/16 17:11
 * @description:  线路发布
 */
public class LineReleasePresenter implements LineReleaseContract.Presenter {

    private Activity activity;
    private LineReleaseContract.Model releaseMode;

    public LineReleasePresenter(Activity activity) {
        this.activity = activity;
        releaseMode = new LineReleaseModel(activity);
    }

    @Override
    public void releaseLine(LineInfoEntity lineInfo, String userId, String updateDetails, int updateMode) {
        if (StringUtil.isEmpty(updateDetails)&&lineInfo.getLineVer()>0){
            MessageBox.show(activity,"请输入更新详情");
            return;
        }
        //首次发布设置范围为全部用户
        if (lineInfo.getLineVer() == 0){
            lineInfo.setUseRange(2);
        }
        String message;
        if (lineInfo.getLineVer()<lineInfo.getLineVerLast()){
            message = "确定将版本回退到V"+lineInfo.getLineVer()+"吗?";
        }else {
            message ="发布前请验证，避免用户更新导致无法正常使用， 确定现在发布?";
        }
        CommonDialog.showTwoBtn(activity,"操作确认",message
                ,"取消","发布",(dialog, which) -> {
                    if (which == -2) {

                    } else {
                        LoadDialog.show(activity,"正在发布线路数据!");

                        releaseMode.publishLine(lineInfo,userId,updateDetails,updateMode,new CommonCallBack() {
                            @Override
                            public void success(Object result) {
                                lineInfo.setLineVer(lineInfo.getLineVerLast()+1);
                                GradeDbController.getInstance(false).update(lineInfo);
                                LoadDialog.dismiss(activity);
                                MessageBox.MessageAndVoiceModel(activity,"线路发布成功!");
                                activity.finish();
                                activity.startActivity(new Intent(activity, PracticeCarSetActivity.class));
                            }

                            @Override
                            public void fail(String error) {
                                LoadDialog.dismiss(activity);
                                activity.runOnUiThread(() ->  CommonDialog.showOneBtn(activity, "操作提示", "线路数据发布失败!", "我知道了", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }));

                            }
                        });
                    }
                    dialog.dismiss();

                });

    }
}
